package com.smart.controller;

import com.smart.common.WebResponse;
import com.smart.common.PageInfo;
import com.smart.common.annotation.WebAccess;
import com.smart.common.domain.BaseEntity;
import com.smart.model.request.OperateUserAddRequest;
import com.smart.model.request.OperateUserRequest;
import com.smart.model.request.OperateUserUpDatePasswordRequest;
import com.smart.model.request.OperateUserUpDateStatusRequest;
import com.smart.model.request.OperateUserUpdateRequest;
import com.smart.service.OperateUserService;
import com.smart.model.vo.OperateUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"运营平台#运营人员模块"})
public class OperateUserController {

    @Autowired
    private OperateUserService operateUserService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<OperateUserVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(operateUserService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<OperateUserVO>> pageQuery(@RequestBody OperateUserRequest request) {
        return WebResponse.build(operateUserService.pageQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("新增")
    @PostMapping("/add")
    public WebResponse<Boolean> add(@Valid @RequestBody OperateUserAddRequest request) {
        return WebResponse.build(operateUserService.add(request));
    }

    @WebAccess(login = true)
    @ApiOperation("编辑")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody OperateUserUpdateRequest request) {
        return WebResponse.build(operateUserService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public WebResponse<Boolean> updatePasswordByKey(@Valid @RequestBody OperateUserUpDatePasswordRequest request) {
        return WebResponse.build(operateUserService.updatePasswordByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("启用/禁用")
    @PostMapping("/updateStatus")
    public WebResponse<Boolean> updateStatus(@Valid @RequestBody OperateUserUpDateStatusRequest request) {
        return WebResponse.build(operateUserService.updateStatus(request));
    }

    @WebAccess(login = true)
    @ApiOperation("删除")
    @PostMapping("/delete/{id}")
    public WebResponse<Boolean> delete(@PathVariable("id") long id) {
        operateUserService.lambdaUpdate().set(BaseEntity::getDeleted, 1).eq(BaseEntity::getId, id).update();
        return WebResponse.build(true);
    }
}
